package com.lc.cehui.ui.fragment;

import android.content.Intent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppFragment;
import com.lc.cehui.base.BaseDialog;
import com.lc.cehui.dialog.MenuDialog;
import com.lc.cehui.http.request.GoodsTypeApi;
import com.lc.cehui.http.request.SendsRuleApi;
import com.lc.cehui.http.response.GoodsTypeBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.MainActivity;
import com.lc.cehui.ui.activity.release.CorporateNewsActivity;
import com.lc.cehui.ui.activity.release.CreateItemActivity;
import com.lc.cehui.ui.activity.release.EnterpriseRecruitmentActivity;
import com.lc.cehui.ui.activity.release.ExhibitionInfoActivity;
import com.lc.cehui.ui.activity.release.IntelligenceReleeaseActivity;
import com.lc.cehui.ui.activity.release.TrainingInformationActivity;
import com.lc.cehui.ui.adapter.HomePageReleaseGridAdapter;
import com.lc.cehui.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;


/**
 *
 */
public class ReleaseFragment extends AppFragment<MainActivity> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    private List<GoodsTypeBean> lists;
    private HomePageReleaseGridAdapter adapter = new HomePageReleaseGridAdapter(R.layout.adapter_hp_release);

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_release;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        recyclerView.setAdapter(adapter);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {

            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adaptera, @NonNull View view, int position) {
                my_sends_rule(adapter.getData().get(position));

            }
        });
    }

    @Override
    protected void initData() {
      /*  lists = new ArrayList<>();
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_recruit, "企业招聘"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_agency, "资质代办"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_cooperation, "校企合作"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_train, "技能培训"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_information, "企业资讯"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_docking, "项目对接"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_sale, "软件销售"));
        lists.add(new HomePageReleaseGridBean(R.mipmap.publish_parts, "仪器配件"));
        adapter.setNewInstance(lists);
*/

        EasyHttp.post(this)
                .api(new GoodsTypeApi().setVirtual_goods(0)
                        .setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                        .setToken(SPUtil.get(getContext(), IntentKey.TOKEN, "")))
                .request(new HttpCallback<GoodsTypeBean>(this) {
                    @Override
                    public void onSucceed(GoodsTypeBean data) {
                        if (callBackCode(data)) {
                            if (data.getData().size() != 0) {
                                LogUtils.e(JSON.toJSONString(data.getData()));
                                adapter.setNewInstance(data.getData());
                            }
                        }
                    }
                });
    }
    public void my_sends_rule(GoodsTypeBean.DataBean  dataBean ){

        EasyHttp.post(this)
                .api(new SendsRuleApi()
                        .setUserid(SPUtil.get(getContext(), IntentKey.USERID, 0))
                        .setType_pid(dataBean.getType_pid()))
                .request(new HttpCallback<GoodsTypeBean>(this) {
                    @Override
                    public void onSucceed(GoodsTypeBean data) {
                        if (callBackCode(data)) {
                            List<String> lists = new ArrayList<>();
                            for (int i = 0; i < dataBean.getList().size(); i++) {
                                lists.add(dataBean.getList().get(i).getTitle());
                            }
                            showMenuDialog(lists, dataBean.getList(),dataBean.getType_pid());
                        }
                    }
                });
    }
    public void showMenuDialog(List<String> list, List<GoodsTypeBean.DataBean.ListBean> select_list,int type_pid) {
        new MenuDialog.Builder(getActivity())
                .setList(list)
                .setTitle("请选择发布类型")
                .setListener(new MenuDialog.OnListener<String>() {
                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        switch (select_list.get(position).getType_id()) {
                            case 1://企业招聘
                                Intent intent1 = new Intent(getContext(),EnterpriseRecruitmentActivity.class);
                                intent1.putExtra("id",select_list.get(position).getId());
                                intent1.putExtra("type_pid",type_pid);

                                startActivity(intent1);
                                break;
                            case 2://代办管家
                                Intent intent2 = new Intent(getContext(),CreateItemActivity.class);
                                intent2.putExtra("id",select_list.get(position).getId());
                                intent2.putExtra("type_pid",type_pid);
                                startActivity(intent2);
                                break;
                            case 16://企业资讯
                                switch (select_list.get(position).getId()){
                                    case 46:
//                                        展会信息
                                        Intent intent46 = new Intent(getContext(),ExhibitionInfoActivity.class);
                                        intent46.putExtra("id",select_list.get(position).getId());
                                        intent46.putExtra("type_pid",type_pid);
                                        startActivity(intent46);
                                        break;
                                    case 48:
                                        Intent intent48 = new Intent(getContext(), IntelligenceReleeaseActivity.class);
                                        intent48.putExtra("type_id",select_list.get(position).getId()+"");
                                        intent48.putExtra("type_pid",type_pid);
                                        startActivity(intent48);
                                        break;
                                    default:
                                        Intent intent0 = new Intent(getContext(),CorporateNewsActivity.class);
                                        intent0.putExtra("id",select_list.get(position).getId());
                                        intent0.putExtra("type_pid",type_pid);
                                        startActivity(intent0);
                                        break;
                                }

                                break;
                            case 15://技能培训
                                Intent intent15 = new Intent(getContext(),TrainingInformationActivity.class);
                                intent15.putExtra("type_id",select_list.get(position).getId()+"");
                                intent15.putExtra("type_pid",type_pid);
                                startActivity(intent15);
                                break;
                            case 18://软件销售
                                Intent intent18 = new Intent(getContext(),CreateItemActivity.class);
                                intent18.putExtra("type_pid",select_list.get(position).getType_id());
                                intent18.putExtra("id",select_list.get(position).getId());
                                startActivity(intent18);
                                break;
                            case 14://校企合作
                                switch (select_list.get(position).getId()){
                                    case 39:
//                                        企业招聘
                                        Intent intent10 = new Intent(getContext(),EnterpriseRecruitmentActivity.class);
                                        intent10.putExtra("id",select_list.get(position).getId());
                                        intent10.putExtra("type_pid",type_pid);

                                        startActivity(intent10);
                                        break;
                                    case 38:
//                                        校企合作
                                        Intent intent0 = new Intent(getContext(),CorporateNewsActivity.class);
                                        intent0.putExtra("id",select_list.get(position).getId());
                                        intent0.putExtra("type_pid",type_pid);
                                        startActivity(intent0);
                                        break;

                                }
                                break;
                            case 19://仪器专区
                                Intent intent19 = new Intent(getContext(),CreateItemActivity.class);
                                intent19.putExtra("type_pid",select_list.get(position).getType_id());
                                intent19.putExtra("id",select_list.get(position).getId());
                                startActivity(intent19);
                                break;
                            default:
                                Intent intent0 = new Intent(getContext(),CorporateNewsActivity.class);
                                intent0.putExtra("id",select_list.get(position).getId());
                                intent0.putExtra("type_pid",type_pid);
                                startActivity(intent0);
                                break;
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {

                    }
                }).show();
    }

}
